package com.sgcc.pda.lib_tools_database.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.github.yuweiguocn.library.greendao.MigrationHelper;
import com.sgcc.pda.lib_tools_database.bean.ticket.TicketDetailPlan;
import com.sgcc.pda.lib_tools_database.gen.*;
import com.sgcc.pda.lib_tools_database.util.DataBaseConfigTools;

import org.greenrobot.greendao.database.Database;

import java.io.IOException;

public class DBOpenHelper extends DevOpenHelper {

    private String mUserId;

    public DBOpenHelper(Context context, String name, int version, String userId) {
        super(context, name, version);
        this.mUserId = userId;
    }

    public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(Database db) {
        super.onCreate(db);
    }

    @Override
    public void onUpgrade(Database db, int oldVersion, int newVersion) {
        MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
                    @Override
                    public void onCreateAllTables(Database db, boolean ifNotExists) {
                        DaoMaster.createAllTables(db, ifNotExists);
                    }

                    @Override
                    public void onDropAllTables(Database db, boolean ifExists) {
                        DaoMaster.dropAllTables(db, ifExists);
                    }
                },
                BusinessDao.class,
                ControlProcessDao.class,
                MeasureRelationDao.class,
                MeasureRiskDao.class,
                MeasureSummaryDao.class,
                QuestionAnswerDao.class,
                QuestionBankDao.class,
                QuestionRelationDao.class,
                QuestionResultDao.class,
                RiskAcceptRecordDao.class,
                RiskPointMeasureDao.class,
                TypicalExpDao.class,
                VideoStandardDao.class,
                WorkContentDao.class,
                WorkForExtendDao.class,
                AppNoForPlanDao.class,
                AttachDao.class,
                MeetingDetailDao.class,
                MeetingStateDao.class,
                OderTypeDao.class,
                RiskCheckPointDao.class,
                RiskCheckStateDao.class,
                RiskHttpStateDao.class,
                TicketCloseStateDao.class,
                TicketMeasureDao.class,
                TicketMeasureSaveDao.class,
                TicketStateDao.class,
                TicketStateSignDao.class,
                LocalImageDao.class,
                TicketDetailDao.class,
                TicketDetailPlanDao.class,
                TicketMeetingDao.class,
                TicketPermitDao.class,
                TicketForTypeDao.class);
        try {
            DataBaseConfigTools.createText(mUserId, newVersion + "");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
